package com.linsen.gateway.handler;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
import com.linsen.core.common.util.Result;
import org.springframework.stereotype.Component;

/**
 * 异常处理方法：每次setAuth函数出现异常时进入
 *
 * @author zhaoyong
 * @date 2023/8/4
 */
@Component
public class SaTokenErrorHandler implements SaFilterErrorStrategy {
    @Override
    public Object run(Throwable e) {
        if (e instanceof NotLoginException) {
            return Result.get(403, e.getMessage());
        }

        if (e instanceof NotPermissionException) {
            return Result.get(401, e.getMessage());
        }

        if (e instanceof NotRoleException) {
            return Result.get(401, e.getMessage());
        }

        return Result.error(e.getMessage());
    }
}
